package jardinzen;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.util.string.Strings;

import plantasvszombies.UpgradePlantPage;
import applicationModels.Juego;
import appmodel.ZenAppModel;
import dominio.Planta;

@SuppressWarnings("serial")
public class JardinZenPage extends WebPage implements Serializable {
	
	private ZenAppModel zenModel;
	
	private WebPage mainPage;
	private Juego juego;
	
	public JardinZenPage(WebPage mainPage, Juego juego) {
		this.mainPage = mainPage;
		this.juego = juego;
		//this.zenModel = new ZenAppModel(juego.getUsuario().getJardinZen());
		this.zenModel = new ZenAppModel();
		
		this.addLinkToIndex();
		
		this.add(new Label("typeJardin", new PropertyModel<String>(this.zenModel, "type")));
		this.add(new Label("puntos",new PropertyModel<Integer>(this.juego.getUsuario(), "puntos")));
		
		this.addIrAlOtroJardinButton();
		
		this.addSearchForm();
		
		this.addResultSearch();
	}

	@SuppressWarnings("rawtypes")
	protected void addLinkToIndex() {
		
		Link index = new Link("indexlink") {
			@Override
			public void onClick() {
				this.setResponsePage(JardinZenPage.this.mainPage);
			}
		};
		
		this.add(index);
	}

	@SuppressWarnings("rawtypes")
	private void addIrAlOtroJardinButton() {
		Link irAlOtroJardin = new Link("irAlOtroJardinButton") {
			@Override
			public void onClick() {
				JardinZenPage.this.zenModel.irAlOtroJardin();
			}
		};
		
		this.add(irAlOtroJardin);
	}
	
	@SuppressWarnings({"rawtypes", "serial", "unchecked"})
	private void addSearchForm() {
		Form<ZenAppModel> form = new Form<ZenAppModel>("searchForm", 
				new CompoundPropertyModel<ZenAppModel>(this.zenModel));
		//setModelObject(new ZenAppModel());
		
		form.add(new AutoCompleteTextField<String>("txtBuscado"){
			@Override
            protected Iterator<String> getChoices(String input)
            {
                if (Strings.isEmpty(input))
                {
                    List<String> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                List<String> choices = new ArrayList<String>(10);

                List<Planta> plantas = zenModel.getPlantas();

                for (final Planta planta : plantas)
                {
                    final String plantaName = planta.getNombre();

                    if (plantaName.toUpperCase().contains(input.toUpperCase()))
                    {
                        choices.add(plantaName);
                        if (choices.size() == 10)
                        {
                            break;
                        }
                    }
                }

                return choices.iterator();
            }
		}); 
		
		RadioGroup orden = new RadioGroup("criterio");
			orden.add(new Radio("1", new Model("1")));
			orden.add(new Radio("-1", new Model("-1")));
		form.add(orden);
			
		RadioGroup propiedadBusqueda = new RadioGroup<ZenAppModel>("propiedad");
			propiedadBusqueda.add(new Radio("nombre", new Model("nombre")));
			propiedadBusqueda.add(new Radio("ataque", new Model("ataque")));
			propiedadBusqueda.add(new Radio("defensa", new Model("defensa")));
		form.add(propiedadBusqueda);
			
		form.add(new Button("buscar") {
			@Override
			public void onSubmit() {
				JardinZenPage.this.zenModel.buscar();
			}
		});
		
		this.add(form);
	}
	
	@SuppressWarnings("rawtypes")
	protected void addResultSearch() {
		WebMarkupContainer panel = new WebMarkupContainer("resultadoPlantas");
		panel.setOutputMarkupId(true);
		
		panel.add(new PropertyListView<Planta>("plantaResultado", new PropertyModel<List<Planta>>(this.zenModel,"plantas")) {
				@Override
				protected void populateItem(final ListItem<Planta> item) {
					
					item.add(new Image("imgPlanta", new ContextRelativeResource("plantas/"+item.getModelObject().getNombre()+".png")));
					
					item.add(new Label("nombrePlanta", item.getModelObject().getNombre()));

					item.add(new Label("ptsPlanta", item.getModelObject().getDetalles()));
					
					item.add(new Link("buttonMejorar") {
						@Override
						public void onClick() {
							this.setResponsePage(new UpgradePlantPage(JardinZenPage.this.mainPage, JardinZenPage.this.juego, item.getModelObject()));
						}
					});
				}
		});
		
		this.add(panel);
	}
}
